# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from permission.models import Permission,View_permission
from django.shortcuts import render
from models import Admin
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from models import Admin
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse,JsonResponse
from utils.permission.check_permission import check_per
# Create your views here.

@check_per("permission_index")
def permission_index(request):
    if request.method == 'GET':
        return render(request, 'permission.html')


def add_user_permission(request):
    if request.method == 'post':

        user_id = request.POST.get('user_id', None)
        user = Admin.objects.filter(id=user_id).first()
        permission_id = request.POST.get('permission_id', None)
        permission = Permission.objects.filter(id=permission_id).first()
        permission.user.add(user)
        permission.save()
        return redirect('//')


def del_user_permission(request):
    if request.method == 'get':

        user_id = request.GET.get('user_id', None)
        user = Admin.objects.filter(id=user_id).first()
        permission_id = request.GET.get('permission_id', None)
        permission = Permission.objects.filter(id=permission_id).first()
        permission.user.remove(user)
        permission.save()
        return redirect('//')


def add_permission(request):
    if request.method == 'post':

        permission_name = request.POST.get('permission_name', None)
        permission = Permission.objects.filter(permission_name=permission_name).first()
        if permission:
            err = u"已有此权限名"
            return redirect('//', locals())
        permission.permission_name = permission_name
        permission.save()
        return redirect('//')


def del_permission(request):
    if request.method == 'post':

        permission_id = request.POST.get('permission_name', None)
        permission = Permission.objects.filter(id=permission_id).first()
        if not permission:
            err = u"没有此权限名"
            return redirect(request, '//', locals())
        permission.delete()
        permission.save()
        return redirect('//')


def add_view_permission(request):
    if request.method == 'post':

        permission_id = request.POST.get('permission_name', None)
        view_id = request.POST.get('view_id', None)
        view = View_permission.objects.filter(id=view_id).first()
        view.view_permiss.add(permission_id)
        view.save()
        return redirect('//')


def del_view_permission(request):
    if request.method == 'post':

        permission_id = request.POST.get('permission_name', None)
        view_id = request.POST.get('view_id', None)
        view = View_permission.objects.filter(id=view_id).first()
        if not view:
            err = u"没有这个视图"
            return redirect(request, '//', locals())
        view.view_permiss.remove(permission_id)
        view.save()
        return redirect('//')
